package cn.kjcoder.redis.task;

import cn.kjcoder.redis.domain.WeiboList;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

/**
 * @author hkj
 * @email 1349091387@qq.com
 * @date 2022/1/21
 * 源码允许修改，并且本人十分乐意有 大佬 来对代码进行 随 意 调 教
 **/
@Service
@Slf4j
public class WeiboListService {

    @Autowired
    private RedisTemplate redisTemplate;
    public static final String RECOMMENDED_KEY = "list:recommended";

    @PostConstruct
    public void init(){
        log.info("数据初始化：榜单");
        List<WeiboList> list =  this.listRecommended();
        this.redisTemplate.delete(RECOMMENDED_KEY);
        list.forEach(t -> this.redisTemplate.opsForSet().add(RECOMMENDED_KEY,t));
    }

    /**
     * 模拟10个热门榜单，用于推荐
     * 每个榜单下有3个用户
     * @return
     */
    public List<WeiboList> listRecommended() {
        List<WeiboList> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            WeiboList weiboList = new WeiboList();
            weiboList.setId(i);
            weiboList.setName("榜单："+i);
            List<String> users = new ArrayList<>();
            for (int j = 0; j < 3; j++) {
                users.add("user:" + j);
            }
            weiboList.setUsers(users);
            list.add(weiboList);
        }
        return list;
    }
}
